/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of GW-Android.

GW-Android is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

GW-Android is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GW-Android. If not, see <http://www.gnu.org/licenses/>. */
package br.ufes.cwtools.android.components;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import br.ufes.cwtools.android.Component;
import br.ufes.cwtools.android.R;

public class Binomios extends Component {
	private final static String TAG = "Binomios";
	
	private List<Binomio> binomios = null;
	
	private static final String[] BINOMIOS_TESTE = {
		"Opaco/Transparente", "Colorido/Monocromático", "Claro/Escuro",
		"Brilhante/Fosco", "Côncavo/Convexo", "Saliências/Reentrâncias",
		"Ortogonal/Curva", "Perpendicular/Inclinada", "Regular/Irregular",
		"Simétrica/Assimétrica", "Complexa/Simples", "Unitária/Fragmentada"
	};
	
	// Chaves para o Map que armazena os dados na View através de setTag().
	private static final String KEY_LEFT_TEXT_VIEW = "leftTextView";
	private static final String KEY_RIGHT_TEXT_VIEW = "rightTextView";
	private static final String KEY_BINOMIO = "binomio";
	
	@SuppressWarnings("unchecked")  // Por causa do cast com generics.
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.binomios);
		LinearLayout container = (LinearLayout) this.findViewById(R.id.binomios_container);
		
		binomios = (List<Binomio>) getLastNonConfigurationInstance();
		
		if (binomios == null) {
			// TODO Carregar os binomios do XML de configuração.
			binomios = new LinkedList<Binomio>(); 
			for (String name : BINOMIOS_TESTE) {
				String[] s = name.split("/");
				if (s.length == 2)
					binomios.add(new Binomio(s[0], s[1]));
			}
		}
		
		for (Binomio binomio : binomios) {
			View v = getLayoutInflater().inflate(R.layout.binomio, null);
			container.addView(v);
			((TextView) v.findViewById(R.id.label_left)).setText(binomio.getLeft());
			((TextView) v.findViewById(R.id.label_right)).setText(binomio.getRight());
			TextView seekbarLeftValueText = (TextView) v.findViewById(R.id.seekbar_value_left);
			seekbarLeftValueText.setText(Integer.toString(binomio.getLeftValue()));
			TextView seekbarRightValueText = (TextView) v.findViewById(R.id.seekbar_value_right);
			seekbarRightValueText.setText(Integer.toString(binomio.getRightValue()));
			SeekBar seekbar = (SeekBar) v.findViewById(R.id.seekbar);
			seekbar.setHorizontalScrollBarEnabled(true);
			
			HashMap<String, Object> tag = new HashMap<String, Object>(3);
			tag.put(KEY_LEFT_TEXT_VIEW, seekbarLeftValueText);
			tag.put(KEY_RIGHT_TEXT_VIEW, seekbarRightValueText);
			tag.put(KEY_BINOMIO, binomio);
			seekbar.setTag(tag);
			
			seekbar.setProgress(binomio.getLeftValue());
			seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
				@Override
				public void onStopTrackingTouch(SeekBar seekBar) { }
				
				@Override
				public void onStartTrackingTouch(SeekBar seekBar) { }
				
				@Override
				public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
					HashMap<String, Object> tag = (HashMap<String, Object>) seekBar.getTag();
					Binomio binomio = (Binomio) tag.get(KEY_BINOMIO);
					binomio.setLeftValue(progress);
					((TextView) tag.get(KEY_LEFT_TEXT_VIEW)).setText(Integer.toString(binomio.getLeftValue()));
					((TextView) tag.get(KEY_RIGHT_TEXT_VIEW)).setText(Integer.toString(binomio.getRightValue()));
					// O comportamento está identico ao do site do Arquigrafia,
					// mas eu acho que o sentido de crescimento/decrescimento
					// dos binômios parece invertido deste modo.
				}
			});
		}
	}
	
	@Override
	public Object onRetainNonConfigurationInstance() {
		return this.binomios;
	}

	public void doneOnClick(View view) {
		// TODO Processar os valores/enviar pro servidor.
		for (Binomio binomio : binomios) {
			Log.d(TAG, "Name: " + binomio.getRight() + ", value: " + binomio.getRightValue());
		}
		this.finish();
	}
}

class Binomio {
	private String left;
	private String right;
	private int leftValue;
	private static final int DEFAULT_VALUE = 50;
	private static final int MAX_VALUE = 100;
	
	public Binomio(String left, String right) {
		this.left = left;
		this.right = right;
		this.leftValue = DEFAULT_VALUE;
	}
	
	public String getLeft() {
		return left;
	}
	
	public String getRight() {
		return right;
	}
	
	public int getLeftValue() {
		return leftValue;
	}

	public void setLeftValue(int value) {
		this.leftValue = value;
	}
	
	public int getRightValue() {
		return MAX_VALUE - leftValue;
	}
}
